package com.lenovo.leos.letools.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class MyTime {
	private static TimeZone tz;
	private static Calendar ca;
	private static String strYear; 
	private static String strMonth;
	private static String strDate;
	private static String strHour;
	private static String strMinute;
	private static String strSecond;

	private static String getString(int i){
		String s;
		if(i<10){
			s = "0" + String.valueOf(i);	
		}
		else{
			s = String.valueOf(i);
		}	
		return s;
	}
	private static void getHour(){
		if(ca.get(Calendar.AM_PM) == Calendar.AM){
			strHour   = getString(ca.get(Calendar.HOUR));
		}
		else{
			int intHour = ca.get(Calendar.HOUR) + 12;
			strHour   = String.valueOf(intHour);
			
		}
	}
	
	private static void createTime(){
		tz = TimeZone.getDefault();
		ca = Calendar.getInstance(tz);
		strYear  =  getString(ca.get(Calendar.YEAR));
		strMonth  = getString(ca.get(Calendar.MONTH)+1); 
		strDate   = getString(ca.get(Calendar.DATE)); 
		getHour();
		strMinute = getString(ca.get(Calendar.MINUTE));
		strSecond = getString(ca.get(Calendar.SECOND));
	}
	
	public static String getCurrentTime(){
		createTime();
		String SysDate = " " + strMonth + 
		                 "-" + strDate + 
		                 " " + strHour + 
		                 ":" + strMinute + 
		                 ":" + strSecond ;
		return SysDate;		
	}
	
	public static long getLastModifyTime() {
		createTime();
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
			.parse(strYear+"/"+strMonth+"/"+strDate+" "+strHour+":"+strMinute+":"+strSecond);
			
			date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
			.parse("1970/01/01 08:00:00");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        long lastModifyTime = date1.getTime() - date2.getTime() > 0 ? date1.getTime()
	              - date2.getTime() : date2.getTime() - date1.getTime();
	 
		return lastModifyTime/1000;
	}
}
